package tsg.shape;

import java.util.ArrayList;

import tsg.physics.Collision;
import tsg.util.Point;

public class Polygon extends Shape
{
	public Polygon()
	{
		
	}
	
	public Polygon(Point[] points)
	{
		this.addPoints(points);
	}
	
	public void addPoint(Point point)
	{
		_points.add(point);
		
		// Set polygon size
		if(this.getX() > point.getX())
		{
			this.setX(point.getX());
		}
		
		if(this.getX() + this.getWidth() < point.getX())
		{
			this.setWidth(point.getX());
		}
		
		if(this.getY() > point.getY())
		{
			this.setY(point.getY());
		}
		
		if(this.getY() + this.getHeight() < point.getY())
		{
			this.setHeight(point.getY());
		}
	}
	
	public void addPoints(Point[] points)
	{
		for(int i=0; i<points.length; i++)
		{
			this.addPoint(points[i]);
		}
	}
	
	public int getPointCount()
	{
		return _points.size();
	}
	
	public Point[] getPoints()
	{
		return _points.toArray(new Point[_points.size()]);
	}
	
	public Point getPointAt(int index)
	{
		return _points.get(index);
	}
	
	public boolean contains(Point point)
	{
		return Collision.pointLiesInPolygon(point, _points);
	}
	
	private ArrayList<Point> _points	= new ArrayList<Point>();
}








